/**
 * 登录处理
 * liujie
 */
$(document).ready(function(){
	//* validation
	$('.login-form').validate({
    	errorElement: 'label', //default input error message container
    	errorClass: 'help-inline', // default input error message class
    	focusInvalid: false, // do not focus the last invalid input
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            },
            remember: {
                required: false
            }
        },
        messages: {
            username: {
                required: "用户名不能为空."
            },
            password: {
                required: "密码不能为空."
            }
        },

        invalidHandler: function (event, validator) { 
        },

        highlight: function (element) { // hightlight error inputs
            //$(element).closest('.control-group').addClass('error'); // set error class to the control group
        },

        success: function (label) {
            //label.closest('.control-group').removeClass('error');
            //label.remove();
        },

        errorPlacement: function (error, element) {
            //error.addClass('help-block col-xs-12 col-sm-reset inline').insertAfter(element.closest('.input-icon'));
        	if(null != error && error[0].innerText != "" ) {
        		layer.tips(error[0].innerText, $(element) , {guide: 3, time: 3,more:true});
        	}
        },

        submitHandler: function (form) {
        	var username = $("#username").val();
        	var password = $("#password").val();
        	
        	//if (rootPath == "/") rootPath = "";
        	
        	$.ajax({
        		url : rootPath + "/doLogin",
        		type: "post",
        		data : {
        			username : username,
        			password : Base64.encode(password)
        		},
        		beforeSend : function() {
        			$("#login").showLoading();
        		},
        		complete: function () {
        			$("#login").hideLoading();
                },
        		success : function(data) {
        			if (data.success) {
        				window.location.href = rootPath + "/common/index";
        				//window.location.href = rootPath + "/partner/brand/list";
        			} else {
        				$("#msg").html(data.msg);
                    	//$('.alert-error', $('.login-form')).show();
        				$(".alert-danger").show();
        			}
        		},
        		error : function(data) {
        			$("#msg").html(data.msg);
                	//$('.alert-error', $('.login-form')).show();
        			$(".alert-danger").show();
        		}
        	});
        }
    });
});